package com.twu.biblioteca.LibraryContents;

import com.twu.biblioteca.Users.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ppeter on 03/10/2014.
 */
public abstract class GenericCheckoutItem {
    String id;
    User lastCheckedOutBy = null;
    boolean checkedOut;

    public GenericCheckoutItem(String id_){
        this.id = id_;
        this.checkedOut = false;
    }

    public boolean isCheckedOut() {
        return this.checkedOut;
    }

    public void setCheckedOut(boolean state, User user){
        this.lastCheckedOutBy = user;

        this.checkedOut = state;
    }

    public String getId(){
        return this.id;
    }

    public abstract List<String> getInfoColumns();

    public List<Integer> getInfoWidths(){
        List<String> columns = getInfoColumns();
        List<Integer> result = new ArrayList<>();
        for(String column:columns){
            result.add(column.length());
        }
        return result;
    }

    public String getInfo(List<Integer> columnWidths){
        List<String> columns = getInfoColumns();
        String result = "";
        boolean first = true;

        if(columns.size() == columnWidths.size()) {
            for(int i = 0; i < columns.size(); i++){
                if(!first) {
                    result += " | ";
                }
                result += padStringTo(columns.get(i), columnWidths.get(i));
                first = false;
            }
        }
        return result;
    }

    private String padStringTo(String input, int length){
        length -= input.length();

        while(length > 0){
            input += " ";
            length--;
        }
        return input;
    }
}
